<?php

/**
 * 文章管理
 */
class Content extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->helper(array('form', 'url'));
	}

	/**
	 * 去往添加/编辑页面
	 */
	public function toEdit($category_id , $content_id) {

		$data['info'] = "no" ;

		$data['category_id'] = $category_id ;

		$category = $this->category_model->get($category_id);

		$data['category_type'] = $category->category_type_id ;

		if($content_id != "no"){
			$data['cms_content'] = $this->content_model->get($content_id);
		}

		$data['maxOrderNo'] = $this->content_model->getMaxOrderNo($category_id);

		$this->load->view('admin/content/content_view_edit',$data);
	}

	/**
	 * 编辑页面保存提交
	 */
	public function createOrUpdate() {

		$info = "" ;

		$id = $this->input->post('id');

		if($id == "") {

			$content_model = $this->content_model ;

			$content_model->category_id = intval($this->input->post('category_id'));
			$content_model->title = $this->input->post('title');
			$content_model->recommend = intval($this->input->post('recommend'));
			$content_model->order_no = intval($this->input->post('order_no'));
			$content_model->content = $this->input->post('content');

			$content_model->creator = $this->session->userdata('userId');
			$content_model->gmt_create = date("Y-m-d H:i:s", time());

			$this->uploadFile($content_model);

			//保存方法
			$this->content_model->createContent();

			$info = "保存成功" ;

		} else {
			//获取原来文章信息
			$content_model = $this->content_model->get($id);

			$content_model->title = $this->input->post('title');
			$content_model->recommend = intval($this->input->post('recommend'));
			$content_model->order_no = intval($this->input->post('order_no'));
			$content_model->content = $this->input->post('content');

			$content_model->modifier = $this->session->userdata('userId');
			$content_model->gmt_modified = date("Y-m-d H:i:s", time());

			$this->uploadFile($content_model);

			//修改方法
			$this->content_model->updateContent($content_model);

			$info = "保存成功" ;
		}

		//跳到列表页面
		$this->toList(intval($this->input->post('category_id')),$info);
	}

	/**
	 * 转到文章列表页面
	 */
	public function toList($category_id , $info){

		//根据栏目id查询文章
		$data['result_array'] = $this->content_model->getByCategoryId($category_id);

		//栏目id
		$data['category_id'] = $category_id ;

		//提示消息
		$data['info'] = $info ;

		$this->load->view('admin/content/content_view_list', $data);
	}

	/**
	 * 删除文章
	 */
	public function delete($category_id , $content_id){

		$this->content_model->delete($content_id);

		//跳到列表页面
		$this->toList($category_id,"删除成功");
	}

	/**
	 * check if logged in
	 * @param $method

	public function _remap($method) {
		$userName = $this->session->userdata('userName');
		if($userName != null) {
			$adminModel = $this->admin_model->getByUserName($userName);
			if($adminModel != null) {
				$arg = $this->uri->segment(4); // http://localhost/abhx/index.php/{1}/{2}/{3}/{4}
				$this->$method($arg);
				return;
			}
		}

		$data['error_info'] = '请先登录！';
		$this->load->view('admin/login', $data);
	}
	*/

	public function uploadFile($content_model){

		//定义允许上传的文件扩展名
		$ext_arr = array('gif', 'jpg', 'jpeg', 'png', 'bmp');

		if (! empty($_FILES['picture']['name']) ) {

			$save_path = realpath("index.php") ;
			$save_path = str_replace("index.php","upload",$save_path) . "/image/" ;

			//创建文件夹
			if (!file_exists($save_path)) {
				mkdir($save_path);
			}else{
				@chmod($save_path, 0777);
			}

			//先删除旧文件
			if(isset($content_model->picture_url)){
				$oldfile = $save_path . $content_model->picture_url ;
				if (file_exists($oldfile)) {
    				unlink ($oldfile);
				}
			}

			//原文件名
			$file_name = $_FILES['picture']['name'];
			//服务器上临时文件名
			$tmp_name = $_FILES['picture']['tmp_name'];
			//文件大小
			$file_size = $_FILES['picture']['size'];

			//获得文件扩展名
			$temp_arr = explode(".", $file_name);
			$file_ext = array_pop($temp_arr);
			$file_ext = trim($file_ext);
			$file_ext = strtolower($file_ext);
			//检查扩展名
			if (in_array($file_ext, $ext_arr) === false) {
				$info = "上传文件扩展名是不允许的扩展名。\n只允许" . implode(",", $ext_arr) . "格式" ;
			}

			//新文件名
			$new_file_name = date("YmdHis") . '_' . rand(10000, 99999) . '.' . $file_ext;
			//移动文件
			$file_path = $save_path . $new_file_name;
			if (move_uploaded_file($tmp_name, $file_path) === false) {
				$info = "上传文件失败" ;
			}

			$content_model->picture_url = $new_file_name ;
		}
	}
}
